home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / vol15n10.zip / MULTIL.ZIP / MLSRC.ZIP / MSDEV / PROJECTS / MLAUNCH / MLPAGE.CPP < prev    next >
C/C++ Source or Header  |  1996-05-04  |  5KB  |  178 lines

  1. // MLPage.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "MFCML.h"
  6. #include "MLPage.h"
  7. #include "resource.h"
  8.  
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14.  
  15. // Forward references
  16. CString GetFilenameExtension(CString);
  17. void CreateRegValue(HKEY, CString, CString);
  18. HKEY CreateRegKey(CString, CString);
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CMLPage property page
  22.  
  23. IMPLEMENT_DYNCREATE(CMLPage, CPropertyPage)
  24.  
  25. CMLPage::CMLPage() : CPropertyPage(CMLPage::IDD)
  26. {
  27.     pRegistryKey = NULL;
  28.  
  29.     m_bOpenDialog = FALSE;
  30.     m_bAboutDialog = FALSE;
  31.     
  32.     //{{AFX_DATA_INIT(CMLPage)
  33.         // NOTE: the ClassWizard will add member initialization here
  34.     //}}AFX_DATA_INIT
  35. }
  36.  
  37. CMLPage::~CMLPage()
  38. {
  39.     if (pRegistryKey != NULL)
  40.         delete pRegistryKey;
  41. }
  42.  
  43. void CMLPage::DoDataExchange(CDataExchange* pDX)
  44. {
  45.     CPropertyPage::DoDataExchange(pDX);
  46.     //{{AFX_DATA_MAP(CMLPage)
  47.     DDX_Control(pDX, IDC_LISTBOX, m_ListBox);
  48.     //}}AFX_DATA_MAP
  49. }
  50.  
  51. BEGIN_MESSAGE_MAP(CMLPage, CPropertyPage)
  52.     //{{AFX_MSG_MAP(CMLPage)
  53.     ON_BN_CLICKED(IDC_ADD, OnAdd)
  54.     ON_BN_CLICKED(IDC_DELETE, OnDelete)
  55.     ON_BN_CLICKED(IDC_ABOUTBOX, OnAboutbox)
  56.     //}}AFX_MSG_MAP
  57. END_MESSAGE_MAP()
  58.  
  59. // Set internal filename
  60. void CMLPage::SetFilename(LPCSTR szShellFilename)
  61. {
  62.     // Initialize the CRegistryKey object with the registry entries
  63.     CString strKey;
  64.  
  65.     strFilename = szShellFilename;
  66.     strKey = GetFilenameExtension(strFilename) + "\\MultiLaunch";
  67.  
  68.     // Construct the RegistryKey object
  69.     pRegistryKey = new CRegistryKey(strKey);
  70. }
  71.  
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CMLPage message handlers
  74.  
  75. // Handle user clicking on the CANCEL button of the property sheet
  76. void CMLPage::OnCancel() 
  77. {
  78.     CPropertyPage::OnCancel();
  79. }
  80.  
  81. // Initialize the Property Page listbox
  82. BOOL CMLPage::OnInitDialog()
  83. {
  84.     // Add registry entries to the listbox
  85.     
  86.     CRegistryValue* pRegistryValue;
  87.     CListBox* pLB = (CListBox*)GetDlgItem(IDC_LISTBOX);
  88.  
  89.     while (pRegistryKey->GetNext(pRegistryValue))
  90.         pLB->AddString(pRegistryValue->m_strName);
  91.     
  92.     return CDialog::OnInitDialog();
  93. }
  94.  
  95. // Delete an association from the registry
  96. void CMLPage::OnDelete() 
  97. {
  98.     HKEY hKey;
  99.     CString strKey;
  100.     int nSelection = m_ListBox.GetCurSel();
  101.     
  102.     if (nSelection != LB_ERR) {
  103.         CString strValueName;
  104.         m_ListBox.GetText(nSelection, strValueName);
  105.  
  106.         // Delete item from list box
  107.         m_ListBox.DeleteString(nSelection);
  108.  
  109.         // Delete item from the registry
  110.         strKey = GetFilenameExtension(strFilename) + "\\MultiLaunch";
  111.  
  112.         if (RegOpenKeyEx(HKEY_CLASSES_ROOT, strKey, 0, KEY_ALL_ACCESS,
  113.             &hKey) == ERROR_SUCCESS)
  114.             RegDeleteValue(hKey, strValueName);
  115.  
  116.         // Delete item from our internal CRegistryKey list
  117.         pRegistryKey->RemoveAt(nSelection);
  118.         
  119.         // If this is the last item in the list, remove the MultiLaunch key
  120.         if (pRegistryKey->GetCount() <= 1)
  121.             RegDeleteKey(HKEY_CLASSES_ROOT, strKey);    
  122.         
  123.     }
  124. }
  125.  
  126. // Add a new association to the registry
  127. void CMLPage::OnAdd() 
  128. {
  129.     HKEY hKey;
  130.     CString strKey;
  131.     DWORD Disposition;
  132.     
  133.     // Execute this code only if we're not already displaying a dialog
  134.     if (!m_bOpenDialog) {
  135.  
  136.         m_bOpenDialog = TRUE;
  137.  
  138.         CFileDialog FileDialog(TRUE, "*.exe", NULL, OFN_FILEMUSTEXIST
  139.             | OFN_PATHMUSTEXIST, "Executable Files(*.exe)|*.exe|COM Files(*.com)|*.com|");
  140.  
  141.         if (FileDialog.DoModal() == IDOK) {
  142.  
  143.             // Does item exist already in the list box?
  144.             if (m_ListBox.FindString(-1, FileDialog.GetFileTitle()) == LB_ERR) {
  145.  
  146.                 // Add item to list box
  147.                 m_ListBox.AddString(FileDialog.GetFileTitle());
  148.  
  149.                 // Add item to the registry
  150.                 strKey = GetFilenameExtension(strFilename) + "\\MultiLaunch";
  151.  
  152.                 if (RegOpenKeyEx(HKEY_CLASSES_ROOT, strKey, 0, KEY_ALL_ACCESS,
  153.                     &hKey) == ERROR_SUCCESS)
  154.                     CreateRegValue(hKey, FileDialog.GetFileTitle(), FileDialog.GetPathName());
  155.                 else {
  156.  
  157.                     // Key did not exist already - create it
  158.                     RegCreateKeyEx(HKEY_CLASSES_ROOT, strKey, 0, NULL,
  159.                         REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL,
  160.                         &hKey, &Disposition);
  161.                     CreateRegValue(hKey, FileDialog.GetFileTitle(), FileDialog.GetPathName());
  162.                 }        
  163.             }
  164.         }
  165.         m_bOpenDialog = FALSE;
  166.     }
  167. }
  168.  
  169. // Display about box
  170. void CMLPage::OnAboutbox() 
  171. {
  172.     if (!m_bAboutDialog) {
  173.         m_bAboutDialog = TRUE;
  174.         ::MessageBox(0, "MultiLaunch V1.0\n\n(C)1996 By Ziff-Davis Publishing Company. All rights reserved.\nFirst appeared in PC Magazine, U.S. Edition, May 28, 1996.\n\nWritten by John Lam.", "About MultiLaunch", MB_OK);
  175.         m_bAboutDialog = FALSE;
  176.     }
  177. }
  178.